#!/usr/bin/env python
import urllib
import urllib2
import httplib
from xml.dom import minidom

modificacion = """<xupdate:modifications version="1.0"  xmlns:xupdate="http://www.xmldb.org/xupdate">
<xupdate:insert-before select="//item[1]" >  
	<xupdate:element name="item">
		<xupdate:element name="title">El origen de Python by Julio</xupdate:element>
		<xupdate:element name="link">http://linkinventado.com</xupdate:element>
	</xupdate:element>  
</xupdate:insert-before> 
</xupdate:modifications>""" 

conn = httplib.HTTP("localhost:8080")
conn.putrequest("POST", "/exist/rest/db/rss")
conn.putheader('Content-Type', 'text/xml')
clen = len(modificacion)
conn.putheader('Content-Length', `clen`)
conn.endheaders()
conn.send(modificacion)
response = conn.getreply()
print response
conn.close()


parametros = urllib.urlencode({"_query":"//item/link","_wrap":"yes","_indent":"yes"})
respuesta = urllib2.urlopen("http://localhost:8080/exist/rest/db/rss/?"+parametros)

parametros = urllib.urlencode({"_query":"//item/title","_wrap":"yes","_indent":"yes"})
respuesta2 = urllib2.urlopen("http://localhost:8080/exist/rest/db/rss/?"+parametros)

salida = minidom.parseString(respuesta.read())
salida2 = minidom.parseString(respuesta2.read())

for i in salida.getElementsByTagName('link'):
	print i.childNodes[0].nodeValue
	
for i in salida2.getElementsByTagName('title'):
	print i.childNodes[0].nodeValue
